package net.takela.auth.login.service.impl;


import jakarta.servlet.http.Cookie;
import jakarta.servlet.http.HttpServletRequest;
import net.takela.common.utils.CookieUtils;
import net.takela.common.utils.NetworkUtils;
import net.takela.auth.token.model.UserAuthToken;
import net.takela.auth.token.token.AuthTokenManager;
import net.takela.auth.token.token.AuthTokenProperties;
import net.takela.auth.login.dao.UserAuthDao;
import net.takela.auth.login.service.UserAuthService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 *
 * @author baboy
 * @since 1.0.0 08/12/2016
 */
@Service
public class UserAuthServiceImpl implements UserAuthService {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    private final UserAuthDao userAuthDao;
    private final AuthTokenProperties authTokenProperties;

    public UserAuthServiceImpl(UserAuthDao userAuthDao, AuthTokenProperties authTokenProperties, AuthTokenManager authTokenManager) {
        this.userAuthDao = userAuthDao;
        this.authTokenProperties = authTokenProperties;
    }

    private String getPlatformByOs(String os){
        if ("android".equalsIgnoreCase(os) || "ios".equalsIgnoreCase(os)){
            return "app";
        }
        return "other";
    }
    @Override
    public int addAuthToken(UserAuthToken token) {
        return userAuthDao.addAuthToken(token);
    }

    @Override
    public boolean updateAuthToken(UserAuthToken token) {
        return userAuthDao.updateAuthToken(token);
    }


    @Override
    public UserAuthToken getAuthTokenByUUID(String uuid) {
        return userAuthDao.getAuthTokenByUUID(uuid);
    }

    @Override
    public UserAuthToken createUserAuthToken(Long uid, Object user,  HttpServletRequest request) {

        String deviceId = getContextParam(request, "deviceId");
        String deviceName = getContextParam(request,"deviceName");
        if (deviceId == null){
            deviceId = request.getHeader("user-agent");
            deviceName = NetworkUtils.getBrowserName(deviceId);
        }
        String os = getContextParam(request, "os");
        String ip = NetworkUtils.getIpAddress(request);
        String platform = getPlatformByOs(os);
        String version = getContextParam(request, "version");
        String osVersion = getContextParam(request, "osVersion");
        UserAuthToken token = new UserAuthToken();
        token.setUid(uid);
        token.setDomain(authTokenProperties.getDomain());
        token.setPlatform(platform);
        token.setDeviceName(deviceName);
        token.setDeviceId(deviceId);
        token.setVersion(version);
        token.setOs(os);
        token.setOsVersion(osVersion);
        token.setIp(ip);

        token.setStatus(UserAuthToken.STATUS_LOGIN);

        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.add(Calendar.SECOND, authTokenProperties.getExpiration());

        token.setExpireTime(calendar.getTime());
        token.setCreateTime(new Date());
        token.setUuid(UUID.randomUUID().toString());
        return token;
    }
    private String getContextParam(HttpServletRequest request, String k){
        try {
            String v = request.getHeader("X-"+k);
            if(v == null){
                Cookie cookie = CookieUtils.getCookie(request, k);
                if(cookie != null) {
                    v = cookie.getValue();
                }
            }
            if(v == null) {
                v = request.getParameter(k);
            }
            return v;
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

}
